<?php
session_start();
//check if the session 'estado' is a teacher
if ( $_SESSION['estado'] == "teacher_login" ) {

//include the header
include("header.php");
	
	//it runs after pressing the submit button
	if($_SERVER['REQUEST_METHOD'] == 'POST'){

	echo "<div id='login_div2'>";
	echo "<br /><br />";
	
		//have the settings for conect to the database
		require('mysqlsettings.php');

		// Attempt to connect to MySQL
		if ($dbc = @mysql_connect($server, $username, $password)) {
				mysql_set_charset('utf8',$dbc);
				mysql_select_db($database_my_marks, $dbc);
			
			
			$problem = FALSE;
			//if the field group_name is not empty, save the value into variable
			if (!empty($_POST['group_name'])) {
				$group_name = mysql_real_escape_string(trim(strip_tags($_POST['group_name'])), $dbc);
			} else {
				//else, print an error message
				print '<p style="color: red;">Please submit group name.</p>';
				$problem = TRUE;
			}
			
			//if there's no problem, you would do the insertion
			if (!$problem) {

				// Define the query:
				$query = "INSERT INTO groups (GroupName) VALUES ('".$group_name."')";
				
				// Execute the query:
				if (@mysql_query($query, $dbc)) {
					//if the query runs ok, print this message
					print '<p>The group has been added!</p>';
					echo "<a href='CreateGroup.php'>Return</a>";
					
				} else {
					//if the query not runs ok, print an error message
					print '<p style="color: red;">Could not add the entry because:<br />' . mysql_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
				}
			
			} // No problem!
			
			mysql_close($dbc); // Close the connection.

		} else {
			//if there's any problem to do the connection to mysql, print an error message
			print '<p style="color: red;">Could not connect to MySQL:<br />' . mysql_error() . '.</p>';

		}
		
	echo "<br /><br /></div><br />";

	}
	else{
		
		
		echo "<div id='login_div2'>";
		echo "<br /><br />";
		//Mount the form to create group
		echo "<h1>Create Groups</h1><br />";	
		echo "<form action='' method='POST'>";
		echo "<input type='text' name='group_name' />";
		echo "<input type='submit' value='Create group'>";
		echo "<form><br /><br />";
		echo "<br /><br /></div><br />";
		
	}
	
	
}
//check if the session 'estado' is a student
else if ( $_SESSION['estado'] == "student_login" ) {
//if the session 'estado' is a student, include a header of the students that have denied the permision to enter at this site
include("headerstudent.php");

}
//if you don't have a session started, you can't enter at this page
else {

include("noacces.php");

}

//include the footer
include("footer.php");

?>